'''
    Rockaby: a text annotation system
    Copyright 2010 Martin Paul Eve

    This file is part of Rockaby.

    Rockaby is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    Rockaby is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with Rockaby.  If not, see <http://www.gnu.org/licenses/>.

'''

from django.db import models
import rockaby.reference
import rockaby.utils
from django.template.defaultfilters import escape

class RockabySettings(models.Model):
    book_name = models.CharField(blank=True, max_length=500)
    
    def __unicode__(self):
        return 'Settings'

    class Meta:
        verbose_name = 'Setting'

class Part(models.Model):
    id = models.AutoField(primary_key=True, default=0)
    part_number = models.IntegerField(null=True, blank=True, unique=True)
    epigraph = models.TextField(blank=True)
    title = models.TextField(blank=True)
    structure = models.TextField(blank=True)
    other = models.TextField(blank=True)
    name = models.CharField(blank=True, max_length=500)
    
    def __unicode__(self):
        return self.name

class Character(models.Model):
    id = models.AutoField(primary_key=True, default=0)
    name = models.TextField(blank=True)
    regex = models.TextField(blank=True)
    information = models.TextField(blank=True)
    
    def __unicode__(self):
        return self.name

class Episode(models.Model):
    id = models.AutoField(primary_key=True, default=0)
    part = models.ForeignKey(Part, to_field='part_number')
    episode = models.IntegerField(null=True, blank=True)
    setting = models.TextField(blank=True)
    synopsis = models.TextField(blank=True)
    reference = models.TextField(blank=True)
    refpages = models.TextField(blank=True)
    synopsis_generated = models.TextField(editable=False, blank=True)
    synopsis_reftable_generated = models.TextField(editable=False, blank=True)
    
    def save(self):
        # get all characters and index items from database
        characters = Character.objects.all()
        index = ReferenceItem.objects.all()
        
        # generate the setting field and apply formatting tags
        synopsis = rockaby.utils.utils.applystyle('<span class="italic">Episode ' + str(self.episode) + ' of ' + escape(self.part.name) + ' is set ' + escape(self.setting) + '</span></span></p><p class="p1"><span>' + escape(self.synopsis))
        
        # apply links to character names and index items
        synopsis = rockaby.utils.utils.styleCharacters(characters, synopsis)
        synopsis = rockaby.utils.utils.styleIndex(index, synopsis)
        
        # generate bibliographic references
        ReferenceEngine = rockaby.reference.ReferenceEngine(synopsis)
        
        self.synopsis_generated = ReferenceEngine.referencetext
        self.synopsis_reftable_generated = ReferenceEngine.referencetable
        
        super(Episode, self).save() # Call the "real" save() method

    
    def __unicode__(self):
        return 'Part ' + str (self.part.part_number) + ', Episode ' + str(self.episode)

class ReferenceItem(models.Model):
    id = models.AutoField(primary_key=True, default=0)
    name = models.TextField(blank=True)
    regex = models.TextField(blank=True)
    information = models.TextField(blank=True)
    
    def __unicode__(self):
        return self.name


class CharactersInEpisode(models.Model):
    id = models.AutoField(primary_key=True, default=0)
    character = models.ForeignKey(Character)
    episode = models.ForeignKey(Episode)
    pages = models.CommaSeparatedIntegerField(blank=True, max_length=500)
    
    def __unicode__(self):
        return self.character.name + ' in Part ' + str(self.episode.part.part_number) + ', Episode ' + str(self.episode.episode)
    
    class Meta:
        verbose_name = 'Characters in Episode'
    
class ReferencesInEpisode(models.Model):
    id = models.AutoField(primary_key=True, default=0)
    verbose_name = 'References in Episode'
    reference = models.ForeignKey(ReferenceItem)
    episode = models.ForeignKey(Episode)
    pages = models.CommaSeparatedIntegerField(blank=True, max_length=500)
    
    def __unicode__(self):
        return self.reference.name + ' in Part ' + str(self.episode.part.part_number) + ', Episode ' + str(self.episode.episode)
    
    class Meta:
        verbose_name = 'References in Episode'